<template>
  <div class="user">
    <!-- 用户信息 -->
    <el-card class="userInfo">
      <el-form :model="form" :inline="true" ref="form">
        <el-form-item label="用户名称" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="用户邮箱" prop="username">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="form.role_id" placeholder="请选择">
            <el-option label="超级管理员" value="1"></el-option>
            <el-option label="管理员" value="2"></el-option>
            <el-option label="老师" value="3"></el-option>
            <el-option label="学生" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="remove">清除</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addClick">+新增用户</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 用户列表 -->
    <el-card class="userList">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="index" width="60" label="序号">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.limit + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="username" label="用户名"> </el-table-column>
        <el-table-column prop="phone" label="电话"> </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="role" label="角色"> </el-table-column>
        <el-table-column prop="remark" label="备注"> </el-table-column>
        <el-table-column prop="status" label="状态"> </el-table-column>
        <el-table-column prop="address" label="操作">
          <template v-slot="{ row }">
            <el-button type="text" @click="editor(row)">编辑</el-button>
            <el-button type="text" @click="state(row.id)">{{
              row.status === '启用' ? '禁用' : '启用'
            }}</el-button>
            <el-button type="text" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <template>
        <div class="block">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page.page"
            :page-sizes="[1, 5, 10, 20, 50, 100, 200]"
            :page-size="page.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>
      </template>
    </el-card>
    <Add ref="add" @getData="getData"></Add>
  </div>
</template>

<script>
import { userList, userRemove, userStatus } from '@/api/user.js'
import Add from './component/add.vue'
export default {
  components: {
    Add
  },
  data () {
    return {
      form: {
        username: '',
        email: '',
        role_id: ''
      },
      // 用户列表
      tableData: [{}],
      total: 0,
      page: {
        limit: 5,
        page: 1
      }
    }
  },
  created () {
    this.getData()
  },
  methods: {
    // 获取用户列表
    async getData () {
      const res = await userList(this.page)
      // console.log(res)
      this.tableData = res.data.data.items
      this.tableData.forEach((item) => {
        if (item.status === 1) {
          item.status = '启用'
        } else if (item.status === 0) {
          item.status = '禁用'
        }
      })
      this.total = res.data.data.pagination.total
    },
    // 点击新增打开弹窗
    addClick () {
      this.$refs.add.isShow = true
      this.$refs.add.mode = 'add'
    },
    // 页码量改变
    handleSizeChange (limit) {
      this.page.limit = limit
      this.page.page = 1
      this.getData()
    },
    // 页改变
    handleCurrentChange (page) {
      this.page.page = page
      this.getData()
    },

    // 删除
    del (id) {
      this.$confirm('您确定删除该用户吗?', '温馨提示')
        .then(async () => {
          await userRemove(id)
          this.$message.success('删除成功')
          this.getData()
        })
        .catch(() => {})
    },

    // 编辑
    editor (row) {
      this.$refs.add.userInfo = row
      this.$refs.add.isShow = true
      this.$refs.add.mode = 'editor'
    },

    // 设计状态
    state (id) {
      userStatus(id)
      this.$message.success('设计状态成功')
      this.getData()
    },

    // 搜索按钮
    async search () {
      const res = await userList(this.form)
      // console.log(res)
      this.tableData = res.data.data.items
      this.tableData.forEach((item) => {
        if (item.status === 1) {
          item.status = '启用'
        } else if (item.status === 0) {
          item.status = '禁用'
        }
      })
      this.total = res.data.data.pagination.total
    },

    // 清除按钮
    remove () {
      this.form = {
        username: '',
        email: '',
        role_id: ''
      }
      this.getData()
    }
  }
}
</script>

<style lang="less" scoped>
.user {
  // .userInfo {
  //   margin-bottom: 20px;
  // }
  .block {
    text-align: center;
  }
}
</style>
